package com.magic.himalaya.adapters;

import android.content.Context;
import android.graphics.Color;
import android.util.TypedValue;

import com.magic.himalaya.R;

import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

public class IndicatorApdater extends CommonNavigatorAdapter {

    private final String[] indicator_title;

    private OnIndicatorTapClickListener mOnIndicatorTapClickListener;//观察者

    public IndicatorApdater(Context context) {
        //从XML中拿取头部标签栏需要的数据
        indicator_title = context.getResources().getStringArray(R.array.indicator_title);
    }

    /**
     * 获得标签数据总数量
     * @return
     */
    @Override
    public int getCount() {
        if (indicator_title!=null)
            return indicator_title.length;
        else
            return 0;
    }

    /**
     * 设置Indicator导航栏的背景和数据以及数据绑定事件
     * @param context
     * @param index
     * @return
     */
    @Override
    public IPagerTitleView getTitleView(Context context, final int index) {
        SimplePagerTitleView simplePagerTitleView = new ColorTransitionPagerTitleView(context);
        simplePagerTitleView.setNormalColor(Color.parseColor("#AAFFFFFF"));
        simplePagerTitleView.setSelectedColor(Color.parseColor("#FFFFFF"));
        simplePagerTitleView.setTextSize(TypedValue.COMPLEX_UNIT_SP,20);
        simplePagerTitleView.setText(indicator_title[index]);
        //导航栏的内容点击事件
        simplePagerTitleView.setOnClickListener(
                //点击上方的内容ITEM时进行调用,同时切换Fragment
                v -> {
                    if(mOnIndicatorTapClickListener!=null)
                        mOnIndicatorTapClickListener.onTabClick(index);
                }
        );
        return simplePagerTitleView;
    }

    /**
     * 获得Indicator指示器(下方的横线)
     * @param context
     * @return
     */
    @Override
    public IPagerIndicator getIndicator(Context context) {
        LinePagerIndicator linePagerIndicator = new LinePagerIndicator(context); //创建indicator指示器
        linePagerIndicator.setMode(LinePagerIndicator.MODE_WRAP_CONTENT);  //indicator指示器横线
        linePagerIndicator.setColors(Color.WHITE);  //indicator指示器颜色
        return linePagerIndicator;
    }

    public void setOnIndicatorTapClickListener(OnIndicatorTapClickListener onIndicatorTapClickListener){
        this.mOnIndicatorTapClickListener = onIndicatorTapClickListener;
    }

    public interface OnIndicatorTapClickListener{
        void onTabClick(int index);
    }
}
